/**
 * Projet : FoireAuxFiches
 * Fichier: faf.web.DeleteAccountController.java
 *
 * Pierre-olivier Pignon le 11 mars 2011 a 23:29:51 
 * 
 */
package faf.web;

import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import faf.business.UserManager;
import faf.entities.Account;


/**
 * @author Pierre-Olivier Pignon
 * @date 11 mars 2011 a 23:29:51
 * 
 */
public class DeleteAccountController implements Controller{
	
	UserManager userManager;

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	
	@SuppressWarnings("finally")
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		
		Account account;
		long account_id;
		Account loggedUser = (Account) request.getSession().getAttribute("user");
		
		HashMap<String, Object> model = new HashMap<String, Object>();
		
		try {
			account_id = Long.parseLong(request.getParameter("account_id"));
			account = userManager.findAccount(account_id);
			if (account.getId()!=loggedUser.getId()){
				model.put("account",account);
				userManager.deleteAccount(account.getId());
			}else{
				model.put("suicide", 1);
			}
				
			
		}finally{
				return new ModelAndView("deleteAccount", model);
		}
	}
	
}
